package com.hongsuan.pplive.mobileAPI.live;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cnjson.common.web.BaseController;
import com.hongsuan.pplive.mobileAPI.utils.JsonUtils;
import com.hongsuan.pplive.modules.live.entity.LiveSourceList;

@Controller
@RequestMapping("/v2/live")
public class QCloudLiveAPIController extends BaseController {

	private static final String DOMAIN = "live.api.qcloud.com";

	@RequestMapping(value = "/CreateLVBChannel", method = RequestMethod.POST)
	public @ResponseBody String createLVBChannel(@RequestParam("channelName") String channelName,
			@RequestParam(required = false, value = "channelDescribe") String channelDescribe,
			@RequestParam("outputSourceType") Integer outputSourceType,
			@RequestParam(required = false, value = "playerPassword") String playerPassword,
			@RequestParam("sourceList") List<LiveSourceList> sourceList) {

		String action = "CreateLVBChannel";
		StringBuilder sb = new StringBuilder();
		// String context=
		// "https://"+domain+"/v2/index.php?Action="+apiName+"&channelName="+channelName+"&outputSourceType="+outputSourceType+"&sourceList.1.name=video-1999&sourceList.1.type=1&COMMON_PARAMS";

		getAPI(action, sb);
		sb.append("&channelName=");
		sb.append(channelName);
		sb.append("&outputSourceType=");
		sb.append(outputSourceType);
		// &sourceList.1.name=video-1999&sourceList.1.type=1&
		int i = 1;
		for (LiveSourceList list : sourceList) {
			sb.append("sourceList.");
			sb.append(i);
			sb.append(".name=");
			sb.append(list.getName());
			sb.append("&sourceList.");
			sb.append(i);
			sb.append(".type=");
			sb.append(list.getType());
			i++;
		}
		sb.append("&channelDescribe=");
		sb.append(channelDescribe);
		sb.append("&playerPassword=");
		sb.append(playerPassword);
		sb.append("&COMMON_PARAMS");
		return JsonUtils.loadJson(sb.toString());
	}

	@RequestMapping(value = "/DescribeLVBChannelList", method = RequestMethod.GET)
	public @ResponseBody String describeLVBChannelList(
			@RequestParam(required = false, value = "channelStatus") Integer channelStatus,
			@RequestParam(required = false, value = "ascDesc", defaultValue = "1") Integer ascDesc,
			@RequestParam(required = false, value = "pageNo") Integer pageNo,
			@RequestParam(required = false, value = "pageSize") Integer pageSize,
			@RequestParam(required = false, value = "orderBy") String orderBy) {

		String action = "DescribeLVBChannelList";
		StringBuilder sb = new StringBuilder();
		getAPI(action, sb);
		sb.append("&channelStatus=");
		sb.append(channelStatus);
		sb.append("&ascDesc=");
		sb.append(ascDesc);
		sb.append("&pageNo=");
		sb.append(pageNo);
		sb.append("&pageSize=");
		sb.append(pageSize);
		sb.append("&orderBy=");
		sb.append(orderBy);
		return JsonUtils.loadJson(sb.toString());
	}

	private void getAPI(String action, StringBuilder sb) {
		sb.append("https://");
		sb.append(DOMAIN);
		sb.append("/v2/index.php?Action=");
		sb.append(action);
	}

	@RequestMapping(value = "/DescribeLVBChannel", method = RequestMethod.GET)
	public @ResponseBody String describeLVBChannel(@RequestParam("channelId") String channelId) {

		String action = "DescribeLVBChannel";
		StringBuilder sb = new StringBuilder();
		getAPI(action, sb);
		sb.append("&channelId=");
		sb.append(channelId);
		return JsonUtils.loadJson(sb.toString());
	}

	@RequestMapping(value = "/ModifyLVBChannel", method = RequestMethod.POST)
	public @ResponseBody String modifyLVBChannel(@RequestParam("channelId") String channelId,
			@RequestParam("channelName") String channelName) {
		String action = "ModifyLVBChannel";
		StringBuilder sb = new StringBuilder();
		getAPI(action, sb);
		sb.append("&channelId=");
		sb.append(channelId);
		sb.append("&channelName=");
		sb.append(channelName);
		return JsonUtils.loadJson(sb.toString());
	}

	@RequestMapping(value = "/StartLVBChannel", method = RequestMethod.GET)
	public @ResponseBody String startLVBChannel(@RequestParam("channelIds") List<String> channelIds) {
		String action = "StartLVBChannel";
		StringBuilder sb = new StringBuilder();
		getAPI(action, sb);
		int i = 1;
		for (String channel : channelIds) {
			sb.append("&channelIds.");
			sb.append(i);
			sb.append("=");
			sb.append(channel);
			i++;
		}
		return JsonUtils.loadJson(sb.toString());
	}

	@RequestMapping(value = "/StopLVBChannel", method = RequestMethod.GET)
	public @ResponseBody String stopLVBChannel(@RequestParam("channelIds") List<String> channelIds) {
		String action = "StopLVBChannel";
		StringBuilder sb = new StringBuilder();
		getAPI(action, sb);
		int i = 1;
		for (String channel : channelIds) {
			sb.append("&channelIds.");
			sb.append(i);
			sb.append("=");
			sb.append(channel);
			i++;
		}
		return JsonUtils.loadJson(sb.toString());
	}

	@RequestMapping(value = "/DescribeLVBOnlineUsers", method = RequestMethod.GET)
	public @ResponseBody String describeLVBOnlineUsers(@RequestParam("channelIds") List<String> channelIds) {
		String action = "DescribeLVBOnlineUsers";
		StringBuilder sb = new StringBuilder();
		getAPI(action, sb);
		int i = 1;
		for (String channel : channelIds) {
			sb.append("&channelIds.");
			sb.append(i);
			sb.append("=");
			sb.append(channel);
			i++;
		}
		return JsonUtils.loadJson(sb.toString());
	}

	@RequestMapping(value = "/DeleteLVBChannel", method = RequestMethod.POST)
	public @ResponseBody String deleteLVBChannel(@RequestParam("channelIds") List<String> channelIds,@RequestParam("userId") String userId) {
		String action = "DeleteLVBChannel";
		StringBuilder sb = new StringBuilder();
		getAPI(action, sb);
		int i = 1;
		for (String channel : channelIds) {
			sb.append("&channelIds.");
			sb.append(i);
			sb.append("=");
			sb.append(channel);
			i++;
		}
		return JsonUtils.loadJson(sb.toString());
	}
	
	@RequestMapping(value = "/CreateRecord", method = RequestMethod.POST)
	public @ResponseBody String createRecord(@RequestParam("channelId") String channelId,@RequestParam("userId") String userId) {
		String action = "CreateRecord";
		StringBuilder sb = new StringBuilder();
		getAPI(action, sb);
		sb.append("&channelId=");
		sb.append(channelId);
		return JsonUtils.loadJson(sb.toString());
	}

}
